package br.com.naybee.client.views.components;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class BotaoLigarDesligarLampada extends Composite {

	private static BotaoLigarDesligarLampadaUiBinder uiBinder = GWT.create(BotaoLigarDesligarLampadaUiBinder.class);

	interface BotaoLigarDesligarLampadaUiBinder extends UiBinder<Widget, BotaoLigarDesligarLampada> {
	}

	private Model param;
	@UiField
	protected Div btnOff;
	@UiField
	protected Div btnOn;

	public BotaoLigarDesligarLampada() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public BotaoLigarDesligarLampada(Model param) {
		initWidget(uiBinder.createAndBindUi(this));
		this.param = param;
	}

	@UiHandler("btnOff")
	public void onClickBtnOff(ClickEvent event) {
		this.param.add("comando", "off");
		NaybeeService naybeeService = new NaybeeService("comandoAutomacaoFs", "comandoIluminacao");
		naybeeService.post(this.param, new CommHandler() {

			public void onSuccess(CommEvent event) {

			}

			public void onException(NAYException error) {
			}
		});
		alterarButtonOn(false);
		alterarButtonOff(true);
	}

	@UiHandler("btnOn")
	public void onClickBtnOn(ClickEvent event) {
		this.param.add("comando", "on");
		NaybeeService naybeeService = new NaybeeService("comandoAutomacaoFs", "comandoIluminacao");
		naybeeService.post(this.param, new CommHandler() {

			public void onSuccess(CommEvent event) {

			}

			public void onException(NAYException error) {
			}
		});

		alterarButtonOn(true);
		alterarButtonOff(false);
	}

	private void alterarButtonOn(boolean statusLigar) {
		String className = "";
		if (statusLigar) {
			className = btnOn.getClassName().replaceAll("button-on-defaut", "button-on-ativo");
		} else {
			className = btnOn.getClassName().replaceAll("button-on-ativo", "button-on-defaut");
		}
		btnOn.setClassName(className);
	}

	private void alterarButtonOff(boolean statusDesligar) {
		String className = "";
		if (statusDesligar) {
			className = btnOff.getClassName().replaceAll("button-on-defaut", "button-off-ativo");
		} else {
			className = btnOff.getClassName().replaceAll("button-off-ativo", "button-on-defaut");
		}
		btnOff.setClassName(className);
	}

}
